/*	This program produces Appendix Figure E.4 */

***** Set directories 
local dir_clean 	"~/Dropbox/Retirement gaming/clean"
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"


use "`dir_clean'/retirsample_bcw.dta", clear

local listcontrols = " i.t i.self_empl i.ndep_cat_1stobs  i.ciiu1_1stobs " 

keep if (empl==1 &  small_1stobs==0 )| (self_empl==1 )|(empl==1 &  small_1stobs==1)
keep if t<=tm(2013m4)
*Normalize wages for each group
sum W if time_bcw==-1 & empl==1 &  small_1stobs==1
local meanw=r(mean)
g Wz=W/`meanw' 
sum W if time_bcw==-1  &  empl==1 &  small_1stobs==0
local meanw=r(mean) 
replace Wz=W/`meanw' if empl==1 &  small_1stobs==0
sum W if time_bcw==-1 & self_empl==1 
local meanw=r(mean)
replace Wz=W/`meanw' if self_empl==1 
* event-time dummies
tab time_bcw, gen(time_bcw_dums)
* dummies interacted with self_employed
forvalues d = 1(1)13 {
	g self_time`d' = time_bcw_dums`d'* self_empl
}		
* Age dummies interacted with small firm employee
g emplsmall=empl*small_1stobs
forvalues d = 1(1)13 {
	g small_time`d' = time_bcw_dums`d'* emplsmall
}		
* Age dummies interacted with large firm employee
g empllarge=empl*(small_1stobs==0)
forvalues d = 1(1)13 {
	g large_time`d' = time_bcw_dums`d'* empllarge
}		
drop self_time5	small_time5 large_time5
* Regression
estimates clear
eststo: reghdfe Wz self_time* small_time* large_time* , absorb(`listcontrols') vce(cluster i )
* Coefficients and sd
foreach Y in self small large {		
	for any beta sd: gen X_`Y' = .
}		
foreach X in self small large {
	forvalues d = 1(1)13 {
		capture qui replace beta_`X' = _b[`X'_time`d'] if time_bcw==`d'-6
		capture qui replace sd_`X'   = _se[`X'_time`d'] if time_bcw==`d'-6
	}			
	replace beta_`X'=0 if time_bcw==-1 
	replace sd_`X'=0 if time_bcw==-1  
}	
* Sample size
global n=e(N)
global N=e(N_clust1)
g sample=e(sample)
count if sample==1 & self_empl==1 & order==1
global Nself=r(N)
count if sample==1 & empl==1 & small_1stobs==1 & order==1
global Nsmall=r(N)
count if sample==1 & empl==1 & small_1stobs==0 & order==1
global Nlarge=r(N)
* Plot the results
collapse beta* sd*, by(time_bcw)
foreach X in self small large{
	gen sd_top_`X' = beta_`X' + 1.96*sd_`X'
	gen sd_bot_`X' = beta_`X' - 1.96*sd_`X'
	gen sd_top10_`X' = beta_`X' + 1.645*sd_`X'
	gen sd_bot10_`X' = beta_`X' - 1.645*sd_`X'
}
*Self-employed
twoway rcap sd_top_self sd_bot_self time_bcw, ///
	cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1, lc(green))   ysc(r(-0.2 .3)) ylabel(-0.2(0.1).3,grid)   ///
	|| rcap sd_top10_self sd_bot10_self time_bcw, cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) ///
	|| scatter beta_self time_bcw, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Self-employed reported earnings")  note("N self-employed=${Nself}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Start of BCW")   
graph export "`dir_output'/figureE4a.png",  replace 	
*Small firm employees
twoway rcap sd_top_small sd_bot_small time_bcw, ///
	cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1, lc(green))   ysc(r(-0.2 .3)) ylabel(-0.2(0.1).3,grid)   ///
	|| rcap sd_top10_small sd_bot10_small time_bcw, cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) ///
	|| scatter beta_small time_bcw, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Employee reported earnings, firms <10 employees")  note("N Small Firm Employees=${Nsmall}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Start of BCW")   
graph export "`dir_output'/figureE4b.png",  replace 	
*Large firm employees
twoway rcap sd_top_large sd_bot_large time_bcw, ///
	cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) xlabel(-5(1)7)  xline(-1, lc(green))   ysc(r(-0.2 .3)) ylabel(-0.2(0.1).3,grid)   ///
	|| rcap sd_top10_large sd_bot10_large time_bcw,cmissing(n) lwidth(thin) lcolor(navy) lpattern(solid) xsc(r(-5 7)) ///
	|| scatter beta_large  time_bcw, mcolor(navy) scheme(s1color) lcolor(navy) lpattern(solid) yline(0, lcolor(gs2)) ///
	title("Employee reported earnings, firms 10+ employees")  note("N Larger Firm Employees=${Nlarge}") ///
	legend(off) ytitle("Estimated Coefficients") xtitle("Years Relative to Start of BCW")   
graph export "`dir_output'/figureE4c.png",  replace 	




clear all
exit
